exports.index = (req,res) => {

    var u_id       = req.headers.u_id;      //当前用户ID
    var resultData = {};                    //最终结果

    //1.获取当前用户的订单
    function searchUserOrder(cb){

        R.SEND_HTTP(
            req,
            res,
            {
                url: config.external_server.apiAddress + "/organization/search_art_product_order/apo_invite_code/" + u_id + "/sort_order/apo_create_time-desc",
                method : 'POST',
                data   : {
                    fields : `
                    apo_real_pay,
                    apo_no,
                    apo_create_time,
                    apo_contact,
                    apo_phone,
                    apo_product_image,
                    apo_campus_name,
                    apo_school_count,
                    apo_invite_code,
                    apo_type,
                    apo_status`
                }
            },
            function (data) {

                resultData.orderData = data.result.data;

                cb();
                
            }
        )

    }

    //计算总销售数
    function searchTotalSalesMoney(cb){

        var totalMoney = 0.0;
        for(var i=0;i<resultData.orderData.length;i++){
            if(resultData.orderData[i].apo_status == 1){
                totalMoney += parseFloat(resultData.orderData[i].apo_real_pay);
            }
        }

        resultData.totalMoney = totalMoney;

        cb();
    }

    //查询用户阅读数据
    function searchUserRead(cb){

        R.SEND_HTTP(
            req,
            res,
            {
                url: config.external_server.apiAddress + "/organization/search_art_product_user_read_record/apurr_uid/" + u_id + "/sort_order/apurr_create_time-desc",
                method : 'POST'
            },
            function (data) {

                resultData.readData = data.result.data;

                cb();
                
            }
        )

    }

    //查询用户的成交状态 
    function searchUserStatus(cb){

        F.SerialEach(resultData.readData,function(item,index,callback){

            R.SEND_HTTP(
                    req,
                    res,
                    {
                        url: config.external_server.apiAddress + "/organization/search_art_product_order/apo_openid/" + item.apurr_openid + "/apo_status/1/apo_invite_code/" + u_id,
                        method : 'POST',
                        data : {
                            fields : "apo_id"
                        }
                    },
                    function (data) {

                        console.log(data)

                        if(data.result.data.length > 0){
                            item.apurr_status = 1;
                        }else{
                            item.apurr_status = 0;
                        }

                        callback()
                        
                    }
                )
        },cb);
    }

    F.Serial([searchUserOrder,searchTotalSalesMoney,searchUserRead,searchUserStatus],() => {
        RES.successResponse(res,resultData);
    })

}